<template>
	<view>
		<Navbar></Navbar>
		<view class="u-m-30">
			<view v-if="bankHave">
				<view class="u-m-b-40" v-for="item in bankList" :key="item.id" v-show="item.teaBankShow">
					<view class="u-m-b-20 u-flex">
						<u-image width="100rpx" height="100rpx" border-radius="20"
							:src="item.head_img"></u-image>
						<view class="u-flex-1 u-m-l-20 u-flex-col u-row-between" style="height: 100rpx;">
							<view class="u-flex u-row-between u-width-100" >
								<view class="u-flex">
									<view class="u-font-title u-m-r-10">{{item.name}}</view>
									<!-- <view class="u-m-r-10" v-if="![0,'0'].includes(item.sex)">
										<u-icon size="32" :color="item.sex == 1 ? '#4399FF' : '#FF69B1'"
											:name="item.sex == 1 ? 'nanxing' : 'nvxing'" custom-prefix="custom-icon" />
									</view> -->
									<view class="u-m-r-10" v-if="item.is_authentication == 2">
										<u-icon size="32" color="#FB6316" name="renzheng" custom-prefix="custom-icon" />
									</view>
									<view class="u-m-r-10" v-if="item.is_company_user == 1">
										<u-icon size="32" color="#F8494D" name="qiye1" custom-prefix="custom-icon" />
									</view>
									<view class="u-tag-box" :class="item.president_class"
										v-if="[5,6,7,8].includes(item.president_status) && item.president_class">
										{{ item.president_name }}
									</view>
								</view>
							</view>
							<view>
								以下为其转换能量推荐好友
							</view>
						</view>
					</view>
					<GroupBox :list="item.tea_bank" @addPerson="addPerson" @callMobile="callMobile" @goDetail="goDetail"></GroupBox>
				</view>
			</view>
			<view v-else>
				<u-empty text="暂无数据" mode="list" margin-top="120"></u-empty>
			</view>
		</view>
		
		<view style="height: 200rpx;"></view>
		<Bottom :btnText="'添加推荐人'" @confirm="toAddRecommend"></Bottom>
		
		<!-- 添加好友 -->
		<Modal :show="modal.show" :confirmText="modal.confirm" :modalContent="modal.content" :showCancel="modal.showCancel"
			@confirm="modalConfirm" @cancel="modal.show = false"></Modal>
	</view>
</template>

<script>
	import Navbar from '@/components/Navbar.vue'
	import Bottom from '@/components/Bottom.vue'
	import GroupBox from '@/components/GroupBox.vue'
	import Modal from '@/components/Modal.vue'
	import { teaBankList } from '@/network/tea.js'
	import { userInfo, singleIntegral, businessCardApply } from '@/network/api.js'
	import { presidentClass, PresidentLevel, call, TeaStatus } from '@/utils/index.js'
	export default{
		components: {
			Navbar,
			Bottom,
			GroupBox,
			Modal
		},
		data(){
			return{
				bankList:[],
				bankHave: true,
				modal: {
					show: false,
					showCancel: true,
					confirm: '确定',
					content: '',
					isDelete: 0 , // 删除好友
					id: ''
				},
				tea_id: 0,
				user_id: uni.getStorageSync("user_id")
			}
		},
		onLoad(options){
			this.tea_id = options.id
			this.getBankList(options.id)
		},
		mounted(){
			let _this = this
			uni.$on('update',function(){
				_this.bankList.length = 0
				_this.getBankList(_this.tea_id)
			})
		},
		onUnload() {
			uni.$off('update')
		},
		methods:{
			getBankList(id){
				const params = {
					tea_id: id,
					user_id: this.user_id
				}
				teaBankList(params).then(res=>{
					if(res.data.length > 0){
						res.data.forEach(item=>{
							item.president_class = presidentClass(item.president_status)
							item.president_name = this.getPresidentName(item.partner_status,item.president_status)
							item.tea_bank.forEach(item2=>{
								item2.president_class = presidentClass(item2.president_status)
								item2.president_name = this.getPresidentName(item2.partner_status,item2.president_status)
							})
							item.teaBankShow = item.tea_bank.length > 0 ? true : false
							this.bankList.push(item)
						})
					}
					this.bankHave = this.bankList.some(item=>item.tea_bank.length > 0)
				})
			},
			getPresidentName(status1, status2) {
        return TeaStatus(status1, status2)  
				// return PresidentLevel(status)
			},
			goDetail(val){
				uni.navigateTo({
					url: "/teaBureau/teaPersonDetail/teaPersonDetail?id=" + val
				})
			},
			callMobile(val){
				call(val)
			},
			addPerson(id){
				this.modal.id = id
				/* const params = {
					key: "ExchangeCards",
					type: 0
				}
				singleIntegral(params).then(res=>{
					this.modal.content = `交换名片需要消耗${res.data.value}积分，请问是否仍要继续？`
					this.modal.show = true
				}) */
				this.modal.content = `交换名片，对方同意后交换成功`
				this.modal.show = true
			},
			modalConfirm(){
				this.modal.show = false
				userInfo({user_id:this.user_id}).then(res=>{
					let integral = res.data.integral  // 用户积分
					const params = {
						key: "ExchangeCards",
						type: 0
					}
					singleIntegral(params).then(singleRes=>{
						let singleIntegral = singleRes.data.value
						if(parseInt(integral) < parseInt(singleIntegral)){
							uni.showToast({
								title: "积分不足",
								icon: "none"
							})
						}else{
							const applyParams = {
								user_id: this.user_id,
								from_user_id: this.modal.id
							}
							businessCardApply(applyParams).then(applyRes=>{
								uni.showToast({
									title: `${applyRes.msg}`,
									icON: "none"
								})
							})
						}
					})
				})
			},
			toAddRecommend(){
				uni.navigateTo({
				 	url:"/teaBureau/teaBank/teaBank?id=" + this.tea_id
				})
			}
		}
	}
</script>

<style>
</style>